/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.OpenParts.test.functionnal;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.thoughtworks.selenium.Selenium;

/**
 * Selenium functional test of Display View on the OpenParts Showcase
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 26 January 2011
 */
public class SeleniumTestDisplayView {

    @Test
    public void testDisplayView() throws InterruptedException {
        WebDriver driver = new FirefoxDriver();

        String baseUrl = "http://127.0.0.1:8080/";
        Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);

        // load the page and test it's loaded
        selenium.open("/index.html");
        selenium.waitForPageToLoad("30000");
        assertTrue(selenium.isElementPresent("//body[@id='fr-openparts']"));
        assertTrue(selenium.isTextPresent("OpenParts Showcase"));
        
        // loading the page load the showcase module - test showcase module is loaded
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Display View"));
        
        // display the level 1 view (and load displaylevel1 module), and test it's displayed
        assertFalse(selenium.isTextPresent("Level 1 view"));
        assertFalse(selenium.isElementPresent("//head/script[contains(@src, 'displaylevel1/js/script1.js')]"));
        assertFalse(selenium.isElementPresent("//head/link[contains(@href, 'displaylevel1/css/style1.css')]"));
        selenium.click("//body[@id='fr-openparts']/div[2]/div[2]/div/div[2]/div/button[1]");
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Level 1 view"));
        assertTrue(selenium.isElementPresent("//head/script[contains(@src, 'displaylevel1/js/script1.js')]"));
        assertTrue(selenium.isElementPresent("//head/link[contains(@href, 'displaylevel1/css/style1.css')]"));
        
        // display the level 2 view (and load displaylevel2 module), and test it's displayed
        assertFalse(selenium.isTextPresent("Level 2 view"));
        assertFalse(selenium.isElementPresent("//head/script[contains(@src, 'displaylevel2/js/script2.js')]"));
        assertFalse(selenium.isElementPresent("//head/link[contains(@href, 'displaylevel2/css/style2.css')]"));
        assertFalse(selenium.isTextPresent("level parameter passed: 2"));
        assertFalse(selenium.isTextPresent("display level 1 success"));
        selenium.click("//div[@id='OP-displaylevel1-displaylevel1-display1']/div/div[2]/div/button");
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Level 2 view"));
        assertTrue(selenium.isElementPresent("//head/script[contains(@src, 'displaylevel2/js/script2.js')]"));
        assertTrue(selenium.isElementPresent("//head/link[contains(@href, 'displaylevel2/css/style2.css')]"));

        // check that level 2 view did get its parameters
        assertTrue(selenium.isTextPresent("level parameter passed: 2"));

        // check that level 1 view is still here
        assertTrue(selenium.isTextPresent("Level 1 view"));

        // check that level 1 view did get a OK ACK
        assertTrue(selenium.isTextPresent("display level 1 success"));
        
        // display the level 2 view (and load displaylevel2 module), and test it's displayed
        assertFalse(selenium.isTextPresent("Level 3 view"));
        assertFalse(selenium.isElementPresent("//head/script[contains(@src, 'displaylevel3/js/script3.js')]"));
        assertFalse(selenium.isElementPresent("//head/link[contains(@href, 'displaylevel3/css/style3.css')]"));
        assertFalse(selenium.isTextPresent("display level 2 success"));
        assertFalse(selenium.isTextPresent("Embedded level received: 3"));
        selenium.click("//div[@id='OP-displaylevel2-displaylevel2-display2']/div/div[2]/div/button");
        Thread.sleep(2000);
        assertTrue(selenium.isTextPresent("Level 3 view"));
        assertTrue(selenium.isTextPresent("Level 3 view content"));
        assertTrue(selenium.isElementPresent("//head/script[contains(@src, 'displaylevel3/js/script3.js')]"));
        assertTrue(selenium.isElementPresent("//head/link[contains(@href, 'displaylevel3/css/style3.css')]"));

        // check that level 2 view did get its parameters
        assertTrue(selenium.isTextPresent("Embedded level received: 3"));

        // check that level 1 and 2 views are still here 
        assertTrue(selenium.isTextPresent("Level 1 view"));
        assertTrue(selenium.isTextPresent("Level 2 view"));

        // check that level 2 view did get a OK ACK
        assertTrue(selenium.isTextPresent("display level 2 success"));

        selenium.stop();
    }

}
